#include "mylibrary.h"
#include "ui_mylibrary.h"

#include "register.h"
#include "library.h"


#include "adminsystem.h"
#include "bookdialog.h"
#include "adminsystem.h"

#include <QMessageBox>
#include <QSqlTableModel>
#include <QSqlDatabase>
#include <QSqlError>
#include <QDebug>
#include <QSqlQuery>

myLibrary::myLibrary(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::myLibrary)
{
    ui->setupUi(this);
}

myLibrary::~myLibrary()
{
    delete ui;
}

void myLibrary::on_pushButtonLogin_clicked()
{
#if 0
    if(ui->lineEditUserName->text().isEmpty())
    {
        QMessageBox::warning(this,NULL,"UserName is enpty");
        return;
    }
    else if(ui->lineEditPassword->text().isEmpty())
    {
        QMessageBox::warning(this,NULL,"Password is enpty");
    }
    else if(ui->lineEditUserName->text() == "sun")
    {
        if(ui->lineEditPassword->text() == "123")
        {
            AdminSystem *as = new AdminSystem;
            as->show();
            close();
        }
    }
    else
    {
        if(ui->lineEditUserName->text()=="lin")
        {
            if(ui->lineEditPassword->text()=="123")
            {
                Library *l = new Library();
                l->show();
                close();
            }
        }
    }
#endif
    QSqlQuery sql_query;
    QString name = ui->lineEditAccount->text();

    QString tempstring="select * from user where Account= '"+name+"'";
    qDebug()<<tempstring;
    if(!sql_query.exec(tempstring))
    {
        qDebug()<<sql_query.lastError();
        matchFlag=false;
    }
    else
    {
        while(sql_query.next())
        {
            usr_account = sql_query.value(0).toInt();
            usr_password = sql_query.value(1).toString();
            usr_sex = sql_query.value(2).toString();
            usr_phone = sql_query.value(3).toString();
            usr_email = sql_query.value(4).toString();
            usr_author = sql_query.value(5).toInt();

            qDebug()<<QString("Account=%1    Password=%2     Sex=%3       Phone=%4    Email=%5      Authorization=%6").arg(usr_account).arg(usr_password).arg(usr_sex).arg(usr_phone).arg(usr_email).arg(usr_author);

        }
        if(usr_account == name.toInt())
        {
            matchFlag=true;
            if(usr_password == ui->lineEditPassword->text())
            {
                if(usr_author == 0)
                {
                    Library *b_s = new Library();
                    b_s->show();
                    close();
                }
                else if(usr_author == 1)
                {
                    //跳转到录入用户
                    BookDialog *b_d = new BookDialog();
                    b_d->show();
                    close();
                }
                else if(usr_author == 2)
                {
                    //跳转到管理员用户的界面
                    AdminSystem *a_u = new AdminSystem();
                    a_u->show();
                    close();
                }
            }
            else
            {
                QMessageBox::critical(this, "Error!", "密码错误");
            }
        }
        else
        {
            matchFlag=false;
            QMessageBox::critical(this, "Error", "用户名不存在");
        }
    }

    qDebug()<<matchFlag;

}

void myLibrary::on_pushButtonRegister_clicked()
{
    Register *reg = new Register();
    reg->show();
    close();
}

void myLibrary::on_pushButton_clicked()
{
    close();
}
